/*
 * 文件名: PerMonthWeekDayLinearLayout.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2012-1-18
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2012-1-18
 * @version [Android PABank C01, @2012-1-18]
 * @description 每月每天每周
 */
public class PerMonthWeekDayLinearLayout extends LinearLayout implements
		OnWheelChangedListener, OnWheelScrollListener {
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 每
	 */
	private WheelView per;
	/**
	 * 日
	 */
	private WheelView day;
	/**
	 * 星期
	 */
	private WheelView week;
	/**
	 * 上午下午
	 */
	private WheelView ampm;
	/**
	 * 返回的星期
	 */
	private int mWeek;
	/**
	 * 返回的每条
	 */
	private int mPer;
	/**
	 * 返回的天
	 */
	private int mDay;
	/**
	 * 返回的上午下午
	 */
	private int mAmpm;

	/**
	 * 是否第一次onLayout过
	 */
	private boolean isOnLayout = false;
	/**
	 * 返回的結果
	 * 
	 */
	private int result;
	/**
	 * 当前的日
	 */
	private int curDay;
	/**
	 * 当前的上午下午
	 */
	private int curAmpm;
	/**
	 * 当前的星期
	 */
	private int curWeek;
	/**
	 * 是否隐藏每日的上午下午
	 */
	private boolean isHideAmpm = false;

	/**
	 * 是不是只有星期一到星期五
	 */
	private boolean isFiveDays = false;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public PerMonthWeekDayLinearLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public PerMonthWeekDayLinearLayout(Context context, boolean isHideAmPm) {
		super(context);
		this.context = context;
		this.isHideAmpm = isHideAmPm;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 */
	public PerMonthWeekDayLinearLayout(boolean isFiveDays, Context context) {
		super(context);
		this.context = context;
		this.isFiveDays = isFiveDays;
		init();
	}

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public PerMonthWeekDayLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化
	 */
	private void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View root = inflater.inflate(R.layout.app_widget_per_week_month_day_layout, null);
		root.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		addView(root);

		per = (WheelView) root.findViewById(R.id.per);
		day = (WheelView) root.findViewById(R.id.day);
		week = (WheelView) root.findViewById(R.id.week);
		ampm = (WheelView) root.findViewById(R.id.ampm);

		// Calendar calendar = Calendar.getInstance();
		Calendar calendar = CommonUtils.getCurrentTimeByCalendarAmpm();
		curWeek = calendar.get(Calendar.DAY_OF_WEEK);
		curDay = calendar.get(Calendar.DAY_OF_MONTH);
		if (curWeek - 1 == 0) {
			mWeek = 6;
		} else {
			mWeek = curWeek - 2;
		}
		APPLog.i("fuck", "mWeek:" + mWeek + "curWeek:" + curWeek);
		if (!isHideAmpm) {
			curAmpm = calendar.get(Calendar.AM_PM);
		}
		curWeek = mWeek;
		per.setViewAdapter(new DateArrayAdapter(context, new String[] { "每月",
				"每周", "每日" }, 1));
		per.setCurrentItem(1);
		mPer = 1;
		if (isFiveDays) {
			mWeek = 0;
			week.setViewAdapter(new DateArrayAdapter(context, new String[] {
					"星期一", "星期二", "星期三", "星期四", "星期五" }, mWeek));
		} else if (!isFiveDays) {
			week.setViewAdapter(new DateArrayAdapter(context, new String[] {
					"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }, mWeek));

		}
		week.setCurrentItem(mWeek);

		day.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.DAYS, curDay - 1));
		CalendarDialogCommon.updateDays(context, day ,calendar);
		day.setCurrentItem(curDay - 1);
		if (!isHideAmpm) {
			ampm.setViewAdapter(new DateArrayAdapter(context,
					CalendarDialogCommon.AM_PM, curAmpm));
			ampm.setCurrentItem(curAmpm);
		} else {
			ampm.setViewAdapter(new DateArrayAdapter(context,
					CalendarDialogCommon.BLOCK, 0));
		}

		per.addChangingListener(this);
		day.addChangingListener(this);
		week.addChangingListener(this);
		per.addScrollingListener(this);
		day.addScrollingListener(this);
		week.addScrollingListener(this);
		if (!isHideAmpm) {
			ampm.addChangingListener(this);
			ampm.addScrollingListener(this);
		}

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (!isOnLayout) {
			isOnLayout = true;
			if (onPerMonthWeekDayListener != null) {
				onPerMonthWeekDayListener.onPerMonthWeekDayScrollChange(mPer,
						mWeek);
			}
		}
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @author EX-XUJIAO001
	 * @version [Android PABank C01, 2012-1-18]
	 */
	public interface OnPerMonthWeekDayListener {
		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param per
		 *            每
		 * @param result
		 *            结果
		 */
		void onPerMonthWeekDayScrollChange(int per, int result);

		/**
		 * 
		 * [一句话功能简述]<BR>
		 * [功能详细描述]
		 * 
		 * @param scrollState
		 *            滚动状态
		 */
		void onPerMonthWeekDayScrollEnd(boolean scrollState);
	}

	/**
	 * 回调监听
	 */
	private OnPerMonthWeekDayListener onPerMonthWeekDayListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnPerMonthWeekDayListener
	 */
	public OnPerMonthWeekDayListener getOnPerMonthWeekDayListener() {
		return onPerMonthWeekDayListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onPerMonthWeekDayListener
	 *            回调监听
	 */
	public void setOnPerMonthWeekDayListener(
			OnPerMonthWeekDayListener onPerMonthWeekDayListener) {
		this.onPerMonthWeekDayListener = onPerMonthWeekDayListener;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		if (onPerMonthWeekDayListener != null) {
			onPerMonthWeekDayListener.onPerMonthWeekDayScrollEnd(true);
		}

	}

	@Override
	public void onScrollingFinished(WheelView wheel) {
		if (onPerMonthWeekDayListener != null) {
			onPerMonthWeekDayListener.onPerMonthWeekDayScrollEnd(false);
		}

	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		switch (wheel.getId()) {
		case R.id.per:
			switch (newValue) {
			case 0:
				day.setVisibility(View.VISIBLE);
				week.setVisibility(View.GONE);
				ampm.setVisibility(View.GONE);
				day.setCurrentItem(curDay - 1);
				result = curDay;
				mPer = 0;
				break;
			case 1:
				day.setVisibility(View.GONE);
				week.setVisibility(View.VISIBLE);
				ampm.setVisibility(View.GONE);
				result = curWeek;
				week.setCurrentItem(curWeek);
				mPer = 1;
				break;
			case 2:
				day.setVisibility(View.GONE);
				week.setVisibility(View.GONE);
				ampm.setVisibility(View.VISIBLE);
				if (!isHideAmpm) {
					ampm.setCurrentItem(curAmpm);
					result = curAmpm;
				} else {
					result = -1;
				}
				mPer = 2;
				break;
			default:
				break;
			}
			break;
		case R.id.week:
			mWeek = newValue;
			result = mWeek;
			break;

		case R.id.day:
			mDay = 1 + newValue;
			result = mDay;
			break;
		case R.id.ampm:
			if (!isHideAmpm) {
				mAmpm = newValue;
				result = mAmpm;
			} else {
				result = -1;
			}

			break;
		default:
			break;
		}

		if (onPerMonthWeekDayListener != null) {
			onPerMonthWeekDayListener.onPerMonthWeekDayScrollChange(mPer,
					result);
		}

	}

}
